<?php
/**
* License
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**/
/**
* @author Krzysztof (Balon) Jagiełło <balonyo@gmail.com>
* @copyright 2007 Krzysztof Jagiełło.
* @version 1.0
*/
class Token {
/** Kod na tokenie */
var $_tokenText;
/** Adres folderu czcionek */
var $_fontDir = '';
/** Nazwy czcionek */
'georgia.TTF'
);
/** Typ obrazka */
var $_imgType = 'gif';
/** Długość kodu na tokenie */
var $_length;
/** Kolor tła */
var $_backColor = '#ffffff';
/** Kolor czcionki */
var $_fontColor = '#466900';
/** Kolor elementów tłą */
var $_elemsColor = '#E1E1E1';
/** Szerokość tokena */
var $_width;
/** Wyskość tokena */
var $_height = 25;
/** Obiekt tła */
var $_bgObject;
/** Obrazek tokena */
var $_image;
/** Nazwa klucza sesji z tokenem */
var $_tokenId = 'token';
/** Dozwolone rozszerzenia */
var $_allowedTypes = array( 'gif', 'jpg', 'png'
);
/** Komunikaty błędów */
'wrong_color' => 'Format koloru (%s) jest nieprawidłowy.',
'wrong_object' => 'Podany obiekt nie istnieje.',
'wrong_img_type'=> 'Podany typ ("%s") jest nieprawidłowy.',
'wrong_file' => 'Czcionka "%s" nie istnieje .'
);
/**
* Konstruktor klasy
*
* @param integer $length - długość tekstu na tokenie
*/
function Token( $length, $bgObject ){
$this->_length = $length;
$this->_width = $length * 15 + 10;
$this->_image = imagecreate( $this->_width, $this->_height);
trigger_error( $this->_errorMsg
['wrong_object'], E_USER_WARNING
); $this->_bgObject = $bgObject;
}
/**
* Tworzy token, zapisuje go w sesji i wyświetla.
*/
function getToken(){
// kolorki
$this->_backColor = $this->MakeColor( $this->_image, $this->_backColor );
$this->_fontColor = $this->MakeColor( $this->_image, $this->_fontColor );
$this->_elemsColor = $this->MakeColor( $this->_image, $this->_elemsColor );
$this->Background();
$this->Text();
$_SESSION[$this->_tokenId] = $this->_tokenText;
if( !in_array( $this->_imgType
, $this->_allowedTypes
) ) header( 'Content-type: image/' . $this->_imgType
); $this->Display();
}
/**
* Tworzy tekst na tokenie
*/
function Text(){
for($i = 0; $i < strlen($this->_tokenText
); $i++){ imagettftext
( $this->_image
, rand(14
, 16
), rand(-10
, 10
), rand(3
, 5
) + $i * 15
, 18
+ rand(-3
, 3
), $this->_fontColor
, $this->getFontDir() . $this->_fontFiles
[rand(0
,count($this->_fontFiles
)-1
)], $this->_tokenText
{$i}); }
return true;
}
/**
* Tworzy tło korzystając z podanej klasy tła
*
* @return true
*/
function Background(){
$this->_bgObject->process( &$this->_image, get_object_vars( $this ) );
return true;
}
/**
* Wyświetla token w wybranym formacie
*/
function Display(){
if( !in_array( $this->_imgType
, $this->_allowedTypes
) ) call_user_func( 'image' . $this->_imgType, $this->_image );
}
/**
* Dodaje czcionki to tablicy. Są one potem losowo używane w tokenie.
*/
function assignFonts(){
foreach( $fonts as $font ){
else
$this->_fontsFiles[] = $font;
}
}
/**
* Zwraca katalog z czcionkami
*/
function getFontDir(){
return $this->_fontDir == '' ? '' : $this->_fontDir . '/';
}
/**
* Zwraca kolor dla obrazka
*
* @param resource $img
* @param string $color - kolor w formacie hexagonalny
* @return integer
*/
function MakeColor( $img, $color ){
if( !preg_match('/^[#]{0,1}[a-f0-9]{6}$/i', $color ) )
);
return imagecolorallocate( $img, $return['r'], $return['g'], $return['b']);
}
}
class TokenBackground_Dots {
function process( $image, $params ){
for($i = 0; $i < round($params['_width'] / 1
.5
); $i++) { $x = rand(0
, $params['_width'] ); $y = rand(0
, $params['_height'] );
imageellipse
( $image, $x, $y, rand(2
, 7
), rand(3
, 6
), $params['_elemsColor'] ); $pts[] = $x.'_'.$y;
}
else {
$i--;
}
}
}
}
class TokenBackground_Slashes {
function process( $image, $params ){
$step = 5;
for($i = 0; $i < round($params['_width']) + ($step*10
); $i+= $step) { imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] );
}
}
}
class TokenBackground_Grid {
function process( $image, $params ){
for($i = 0; $i < round($params['_width']); $i+= 5) { // poziome linie
imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] );
// pionowe linie
imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] );
}
}
}
$token = new Token(32, new TokenBackground_Dots );
$token->getToken();
?>
Klasa tokena.
Najważniejsze cechy:
- Proste tworzenie nowych efektów tła,
- duża konfigurowalność działania klasy.
Przykładowe efekty tła:
<?php
/** Ładne kółeczka - zgapiłem z jakiegoś innego tokena ;) */
class TokenBackground_Dots {
function process( $image, $params ){
for($i = 0; $i < round($params['_width'] / 1
.5
); $i++) { $x = rand(0
, $params['_width'] ); $y = rand(0
, $params['_height'] );
imageellipse
( $image, $x, $y, rand(2
, 7
), rand(3
, 6
), $params['_elemsColor'] ); $pts[] = $x.'_'.$y;
}
else {
$i--;
}
}
}
}
/** Ukośne paski */
class TokenBackground_Slashes {
function process( $image, $params ){
$step = 5;
for($i = 0; $i < round($params['_width']) + ($step*10
); $i+= $step) { imageline( $image, $i, 0, 0, $i, $params['_elemsColor'] );
}
}
}
/** Najzwyklejsza kratka */
class TokenBackground_Grid {
function process( $image, $params ){
for($i = 0; $i < round($params['_width']); $i+= 5) { // poziome linie
imageline( $image, 0, $i, $params['_width'], $i, $params['_elemsColor'] );
// pionowe linie
imageline( $image, $i, 0, $i, $params['_height'], $params['_elemsColor'] );
}
}
}
?>
Sposób użycia:
Kod
<?php
// ... tutaj klasa tokena
// ... tutaj klasa efektu
$token = new Token(20, new TokenBackground_Dots );
$token->getToken();
?>
.
Ten post edytował kwiateusz 19.05.2007, 16:19:57